<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="设备编号" prop="deviceNo">
        <el-input
          v-model="queryParams.deviceNo"
          placeholder="请输入设备编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报警级别" prop="level">
        <el-select
          v-model="queryParams.level"
          placeholder="请选择报警级别"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.otu_alarm_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="报警类型" prop="alarmType">
        <el-select
          v-model="queryParams.alarmType"
          placeholder="请选择报警类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.otu_alarm_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:alarm:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:alarm:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
        :columns="columns"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="alarmList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="设备名称"
        align="center"
        prop="device.deviceName"
        width="170"
        v-if="columns[0].visible"
      />
      <el-table-column
        label="设备编号"
        align="center"
        prop="deviceNo"
        width="110"
        v-if="columns[1].visible"
      />
      <el-table-column
        label="通道号"
        align="center"
        prop="channel"
        width="80"
        v-if="columns[2].visible"
      />
      <el-table-column
        label="报警类型"
        align="center"
        prop="alarmType"
        width="80"
        v-if="columns[3].visible"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.otu_alarm_type"
            :value="scope.row.alarmType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="报警等级"
        align="center"
        prop="level"
        width="90"
        v-if="columns[4].visible"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.otu_alarm_level"
            :value="scope.row.level"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="报警详情"
        align="center"
        prop="description"
        v-if="columns[5].visible"
      >
      </el-table-column>
      <el-table-column
        label="报警时间"
        align="center"
        prop="createTime"
        width="180"
        v-if="columns[6].visible"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="150"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:alarm:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listAlarm,
} from "@/api/base/alarm";

export default {
  name: "Alarm",
  dicts: ["otu_alarm_type", "otu_alarm_level", "otu_alarm_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报警记录表格数据
      alarmList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceNo: null,
        alarmType: null,
        level: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 列信息
      columns: [
        { key: 0, label: "设备名称", visible: true },
        { key: 1, label: "设备编号", visible: true },
        { key: 2, label: "通道号", visible: true },
        { key: 3, label: "报警类型", visible: true },
        { key: 4, label: "报警等级", visible: true },
        { key: 5, label: "报警详情", visible: true },
        { key: 6, label: "报警时间", visible: true },
      ],
      // 表单校验
      rules: {
        alarmType: [
          { required: true, message: "消息类型不能为空", trigger: "change" },
        ],
        level: [
          { required: true, message: "消息等级不能为空", trigger: "blur" },
        ],
        description: [
          { required: true, message: "报警明细不能为空", trigger: "blur" },
        ],
        status: [
          {
            required: true,
            message: "0：未处理  1：已处理不能为空",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询报警记录列表 */
    getList() {
      this.loading = true;
      listAlarm(this.queryParams).then((response) => {
        this.alarmList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        alarmId: null,
        deviceId: null,
        deviceNo: null,
        channel: null,
        measureId: null,
        referId: null,
        alarmType: null,
        level: null,
        description: null,
        status: null,
        checkStatus: null,
        checkTime: null,
        checkUserId: null,
        checkRemark: null,
        dealTime: null,
        dealUserId: null,
        dealRemark: null,
        regionId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.alarmId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 && 报警记录不可后台添加*/
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报警记录";
    },
    /** 修改按钮操作 && 报警记录不可后台添加*/
    handleUpdate(row) {
      this.reset();
      const alarmId = row.alarmId || this.ids;
      getAlarm(alarmId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报警记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.alarmId != null) {
            updateAlarm(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAlarm(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const alarmIds = row.alarmId || this.ids;
      this.$modal
        .confirm(i18n.t("public.ConfirmDeletion"))
        .then(function () {
          return delAlarm(alarmIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(i18n.t("public.deleteSuccess"));
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "base/alarm/export",
        {
          ...this.queryParams,
        },
        `alarm_${new Date().getTime()}.xlsx`
      );
    },
    fixFun(number) {
      if (number) {
        return number.toFixed(2);
      } else {
        return "";
      }
    },
    alarmText(item) {
      let that = this;
      if (item.type === "device_network_interruption") {
        //设备  中断报警
        let { createTime, deviceNo } = item;
        return `${i18n.t(
          "globalAlert.NetworkInterruptionAlert1"
        )} ${createTime}， ${i18n.t(
          "globalAlert.NetworkInterruptionAlert2"
        )} ${deviceNo} ${i18n.t("globalAlert.NetworkInterruptionAlert3")}`;
      } else if (item.type === "full_length") {
        //全程长度
        console.log("全程长度", item);
        let {
          deviceNo,
          startMarkName,
          endMarkName,
          channel,
          distance,
          threshold,
          variation,
          circuitName,
        } = item;
        if (endMarkName) {
          return `${i18n.t("globalAlert.FullLengthAlert1")}${deviceNo} ${i18n.t(
            "globalAlert.FullLengthAlert2"
          )} ${channel} ${i18n.t(
            "globalAlert.FullLengthAlert3"
          )} ${circuitName} ${i18n.t(
            "globalAlert.FullLengthAlert4"
          )} ${that.fixFun(variation)} ${i18n.t(
            "globalAlert.FullLengthAlert5"
          )}${that.fixFun(threshold)} ${i18n.t(
            "globalAlert.FullLengthAlert6"
          )} ${startMarkName} ${i18n.t(
            "globalAlert.FullLengthAlert7"
          )} ${endMarkName} ${i18n.t(
            "globalAlert.FullLengthAlert8"
          )} ${startMarkName}  ${that.fixFun(distance)}${i18n.t(
            "globalAlert.FullLengthAlert9"
          )}`;
        } else {
          return `${i18n.t(
            "globalAlert.FullLengthAlert1"
          )} ${deviceNo} ${i18n.t(
            "globalAlert.FullLengthAlert2"
          )} ${channel} ${i18n.t(
            "globalAlert.FullLengthAlert3"
          )} ${circuitName}  ${i18n.t(
            "globalAlert.FullLengthAlert4"
          )}${that.fixFun(variation)}${i18n.t(
            "globalAlert.FullLengthAlert5"
          )}${that.fixFun(threshold)} ${i18n.t(
            "globalAlert.FullLengthAlert6"
          )} ${startMarkName} ${i18n.t("globalAlert.FullLengthAlert10")}`;
        }
      } else if (item.type === "full_atten") {
        //全程衰耗
        let { channel, deviceNo, circuitName, variation, threshold } = item;
        return `${i18n.t("globalAlert.FullAttenuation1")} ${deviceNo} ${i18n.t(
          "globalAlert.FullAttenuation2"
        )} ${channel} ${i18n.t(
          "globalAlert.FullAttenuation3"
        )} ${circuitName} ${i18n.t(
          "globalAlert.FullAttenuation4"
        )}${that.fixFun(variation)}${i18n.t(
          "globalAlert.FullAttenuation5"
        )} ${that.fixFun(threshold)} ${i18n.t("globalAlert.FullAttenuation6")}`;
      }
    },
  },
};
</script>
